#!/bin/bash
# setup-environment.sh - 环境设置和依赖安装脚本

set -e  # 遇到错误时退出

echo \"英语单词听写工具 - 环境设置脚本\"
echo \"=====================================\"

# 检查操作系统
if [[ \"$OSTYPE\" == \"linux-gnu\"* ]]; then
    PLATFORM=\"linux\"
elif [[ \"$OSTYPE\" == \"darwin\"* ]]; then
    PLATFORM=\"macos\"
else
    echo \"不支持的操作系统: $OSTYPE\"
    exit 1
fi

echo \"检测到平台: $PLATFORM\"

# 检查并安装必要的工具
install_dependencies() {
    echo \"正在安装依赖...\"
    
    if [ \"$PLATFORM\" = \"linux\" ]; then
        # 检查是否为基于Debian的系统
        if command -v apt &> /dev/null; then
            echo \"检测到基于Debian的系统，使用apt安装依赖\"
            sudo apt update
            sudo apt install -y libwebkit2gtk-4.0-dev build-essential curl wget libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev libsoup-3.0-dev libjavascriptcoregtk-4.1-dev pkg-config
        # 检查是否为基于Red Hat的系统
        elif command -v dnf &> /dev/null; then
            echo \"检测到基于Red Hat的系统，使用dnf安装依赖\"
            sudo dnf check-update
            sudo dnf install -y webkit2gtk3-devel gcc-c++ make curl wget openssl-devel gtk3-devel libappindicator-gtk3-devel librsvg2-devel
        # 检查是否为基于Arch的系统
        elif command -v pacman &> /dev/null; then
            echo \"检测到基于Arch的系统，使用pacman安装依赖\"
            sudo pacman -Syu --noconfirm webkit2gtk base-devel curl wget openssl gtk3 libappindicator-gtk3 librsvg
        else
            echo \"无法检测到支持的包管理器，请手动安装依赖\"
            echo \"对于Debian/Ubuntu系统，请运行:\"
            echo \"  sudo apt install libwebkit2gtk-4.0-dev build-essential curl wget libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev\"
            echo \"对于RHEL/Fedora系统，请运行:\"
            echo \"  sudo dnf install webkit2gtk3-devel gcc-c++ make curl wget openssl-devel gtk3-devel libappindicator-gtk3-devel librsvg2-devel\"
            echo \"对于Arch系统，请运行:\"
            echo \"  sudo pacman -S webkit2gtk base-devel curl wget openssl gtk3 libappindicator-gtk3 librsvg\"
            exit 1
        fi
    elif [ \"$PLATFORM\" = \"macos\" ]; then
        # macOS - 检查是否安装了Homebrew，如果没有则提示安装
        if ! command -v brew &> /dev/null; then
            echo \"请先安装Homebrew，然后重新运行此脚本\"
            echo \"安装命令: /bin/bash -c \\\"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\\\"\"
            exit 1
        fi
        echo \"macOS系统，使用Homebrew安装依赖...\"
        # macOS通常不需要额外的系统依赖，因为Tauri使用原生WebKit
    fi
}

# 检查Rust是否已安装
install_rust() {
    if ! command -v rustc &> /dev/null; then
        echo \"正在安装Rust...\"
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
        source ~/.cargo/env
    else
        echo \"Rust已安装，版本: $(rustc --version)\"
    fi
    
    # 检查并安装Rust目标
    if ! rustc --print target-list | grep -q \"x86_64-unknown-linux-gnu\"; then
        rustup target add x86_64-unknown-linux-gnu
    fi
    
    if ! rustc --print target-list | grep -q \"aarch64-unknown-linux-gnu\"; then
        rustup target add aarch64-unknown-linux-gnu
    fi
}

# 检查Node.js和npm是否已安装
install_nodejs() {
    if ! command -v node &> /dev/null; then
        echo \"正在安装Node.js和npm...\"
        curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
        sudo apt-get install -y nodejs
    else
        echo \"Node.js已安装，版本: $(node --version)\"
    fi
    
    if ! command -v npm &> /dev/null; then
        echo \"npm未安装，请检查Node.js安装\"
        exit 1
    else
        echo \"npm已安装，版本: $(npm --version)\"
    fi
}

# 检查并安装Tauri CLI
install_tauri_cli() {
    if ! cargo install --list | grep -q \"tauri-cli\"; then
        echo \"正在安装Tauri CLI...\"
        cargo install tauri-cli
    else
        echo \"Tauri CLI已安装\"
    fi
}

# 安装前端依赖
install_frontend_deps() {
    echo \"正在安装前端依赖...\"
    cd frontend
    npm install
    cd ..
}

# 主函数
main() {
    echo \"开始安装环境依赖...\"
    
    # 安装系统依赖
    install_dependencies
    
    # 安装Rust
    install_rust
    
    # 重新加载环境变量以确保Rust可用
    source ~/.cargo/env
    
    # 安装Node.js
    install_nodejs
    
    # 安装Tauri CLI
    install_tauri_cli
    
    # 安装前端依赖
    install_frontend_deps
    
    echo \"环境设置完成！\"
    echo \"您可以使用以下命令运行项目：\"
    echo \"  cd frontend && npm run dev\"
    echo \"或\"
    echo \"  cargo tauri dev\"
    echo \"\"
    echo \"您也可以使用项目脚本进行开发：\"
    echo \"  ./scripts/dev.sh        # 开发模式\"
    echo \"  ./scripts/build.sh      # 构建项目\"
    echo \"  ./scripts/build-web.sh  # 构建Web版本\"
}

main \"$@\"